package com.industrial.im.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSONObject;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.im.domain.MessageHistory;
import com.industrial.im.mapper.MessageHistoryMapper;
import com.industrial.im.service.IMessageHistoryService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class MessageHistoryServiceImpl implements IMessageHistoryService {

    private final MessageHistoryMapper messageHistoryMapper;

    public MessageHistoryServiceImpl(MessageHistoryMapper messageHistoryMapper) {
        this.messageHistoryMapper = messageHistoryMapper;
    }

    /**
     * 同步聊天记录到业务库
     *
     * @param messageHistory
     */
    @Override
    public void messageSync(MessageHistory messageHistory) {
        messageHistory.setBaseId(IdUtil.simpleUUID());
        messageHistory.setBaseCreateTime(new Date());

        String content = messageHistory.getContent();
        if (StringUtils.isNoneBlank(content)) {
            JSONObject contentObject = JSONObject.parseObject(content, JSONObject.class);
            messageHistory.setContent((String) contentObject.get("content"));
            messageHistory.setSendNickName((String) contentObject.get("currentNickName"));
            messageHistory.setToNickName((String) contentObject.get("nickName"));
            messageHistory.setMsgTimestamp((Long) contentObject.get("sendTime"));
        }

        messageHistoryMapper.messageSync(messageHistory);
    }

    /***
     * 导出单聊的消息记录
     * @param history
     */
    @Override
    public List<MessageHistory> exportSingleChatMessage(MessageHistory history) {
        if (StringUtils.isBlank(history.getSendNickName()) || StringUtils.isBlank(history.getToNickName())) {
            throw new RuntimeException("没有找到发送人或者接收人");
        }
        List<MessageHistory> list = messageHistoryMapper.selectSingleChatMessage(history);
        if (list != null && list.size() > 0) {
            list.forEach(item -> {
                Long msgTimestamp = item.getMsgTimestamp();
                DateTime date = DateUtil.date(msgTimestamp);
                String sendMessageTime = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
                item.setMsgTimestampStr(sendMessageTime);
            });
        }
        return list;
    }

    /***
     * 导出群组的聊天记录
     * @param history
     * @return
     */
    @Override
    public List<MessageHistory> exportGroupChatMessage(MessageHistory history) {
        if (StringUtils.isBlank(history.getToUserId())) {
            throw new RuntimeException("没有找到群组Id");
        }
        List<MessageHistory> list = messageHistoryMapper.exportGroupChatMessage(history);
        if (list != null && list.size() > 0) {
            list.forEach(item -> {
                Long msgTimestamp = item.getMsgTimestamp();
                DateTime date = DateUtil.date(msgTimestamp);
                String sendMessageTime = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
                item.setMsgTimestampStr(sendMessageTime);
            });
        }
        return list;
    }
}
